$(document).ready(function(){
	$('input:radio').click(function () { 
		this.blur();
		if($(this).attr('id')=='otherradio'){
			$('#othercontent').removeAttr('readonly');
			$('#othercontent').focus(); 
		}else{
			$('#othercontent').attr('readonly','readonly');
			$('#othercontent').val('');
		}
	}); 
})

function details(hzdh) {
	var $win;
	$win = $('#yhxxDetails').window({
	    title: '详情',
	    width: 1020,
	    height: 450,
//	    top: ($(window).height() - 820) * 0.5,
//	    left: ($(window).width() - 450) * 0.5,
	    shadow: true,
	    modal: true,
//	    iconCls: 'icon-add',
	    closed: true,
	    minimizable: false,
	    maximizable: true,
	    maximized: true,  
	    collapsible: true
	});

	$win.window('open');
	
//	$('#yhxxDetails').window('open');
	$('#dgDetails').datagrid({
		height : backendCommon.getHeight(0.88),
		loadMsg : "数据装载中,请稍等....",
		url : mainDomain + '/btsb/detaillist.action?hzdh='+hzdh,
		nowrap : false,
		rownumbers : true,
		fitColumns : false,
		pagination : true,
		singleSelect : true,
		pageSize : 10,
		pageNumber : 1,
		pageList : [ 10, 15, 20 ],
		columns : [ [/* {
			field : 'hzdh',
			title : '申报编号',
			width : $(this).width() * 0.08,
			align : 'center'
		}, {
			field : 'yslx',
			title : '运输类型',
			width : $(this).width() * 0.05,
			align : 'center',
			formatter : function(value, rec, index) {
				if("1"==value){
					return (!value) ? "暂无" : "铁水";
				}else if("2"==value){
					return (!value) ? "暂无" : "水铁";
				}
			}
		}, {
			field : 'xh',
			title : '箱号',
			width : $(this).width() * 0.08,
			align : 'center'
		}, {
			field : 'hwlx',
			title : '货物类型',
			width : $(this).width() * 0.05,
			align : 'center',
			formatter : function(value, rec, index) {
				return (!rec.hwlxinfoVO) ? "暂无" : rec.hwlxinfoVO.hwlxmc;
			}
		}, {
			field : 'hwmc',
			title : '货物名称',
			width : $(this).width() * 0.08,
			align : 'center'
		}, */{
			field : 'xx',
			title : '箱型',
			width : $(this).width() * 0.05,
			align : 'center',
			formatter : function(value, rec, index) {
				return (!rec.xxinfoVO) ? "暂无" : rec.xxinfoVO.xxmc;
			}
		}, {
			field : 'crtlsj',
			title : '出/入铁路站场时间',
			width : $(this).width() * 0.10,
			align : 'center',
			formatter : function(value, rec, index) {
				return (!value) ? "暂无" : backendCommon.dateTimeToLocalStringDays(value);
			}
		}, {
			field : 'tlhph',
			title : '铁路货票号',
			width : $(this).width() * 0.10,
			align : 'center'
		}, {
			field : 'crgksj',
			title : '出/入港口时间',
			width : $(this).width() * 0.10,
			align : 'center',
			formatter : function(value, rec, index) {
				return (!value) ? "暂无" : backendCommon.dateTimeToLocalStringDays(value);
			}
		}, {
			field : 'hwydh',
			title : '货物运单号',
			width : $(this).width() * 0.12,
			align : 'center'
		}/*, {
			field : 'hgsbdh',
			title : '海关申报单号',
			width : $(this).width() * 0.12,
			align : 'center'
		}, {
			field : 'fh',
			title : '封号',
			width : $(this).width() * 0.05,
			align : 'center'
		}, {
			field : 'sfsc',
			title : '轨迹查询',
			width : $(this).width() * 0.08,
			align : 'center',
			formatter : function(value, rec, index) {
				return "<a href='http://www.whxgedi.gov.cn/cms/control/gisMap' style='color:blue' target='_blank'>轨迹查询</a>";
			}
		}*/, {
			field : 'ishc',
			title : '状态',
			width : $(this).width() * 0.05,
			align : 'center',
			formatter : function(value, rec, index) {
				if(0==value){
					return "待审核";
				}else if(1==value){
					return "通过";
				}else{
					return "未通过";
				}
			}
		}, {
			field : 'jjyy',
			title : '质疑描述',
			width : $(this).width() * 0.15,
			align : 'center'
		}, {
			field : 'id',
			title : '操作',
			width : $(this).width() * 0.23,
			align : 'center',
			formatter : function(value, rec, index) {
				var oprate = '';
				if(rec.ishc==0){
					oprate += '<div><a class="btn_blue06"  id="editbtn" onclick="javascript:setcross(\''
						+ rec.hzdh + '\',\'' + rec.tlhph + '\',\''+ rec.hwydh + '\')" href="javascript:void(0)">通过</a></div>';
					oprate += '<div><a class="btn_blue06"  id="editbtn" onclick="javascript:showzy(\''
						+ rec.hzdh + '\',\'' + rec.tlhph + '\',\''+ rec.hwydh + '\')" href="javascript:void(0)">质疑</a></div>';
				}
				oprate += '<div><a class="btn_blue06"  id="editbtn" onclick="javascript:yddetails(\''
					+ rec.hzdh + '\',\'' + rec.tlhph + '\',\''+ rec.hwydh + '\')" href="javascript:void(0)">查看明细</a></div>';
				
				return oprate;
			}
		} ] ]
	});
}


function doSearch(){
	$('#yhxxDatagrid').datagrid('load',{
		qyqc: $('#qyqc').val()
	});
}

function qyqcFormatter(val, row) {
	if (!val) {
		return val;
	}
	return '<a href="javascript:void(0)" onclick="javascript:details(' + row.id
			+ ');">' + val + '</a>';
}

function qcFormatter(val, row) {
	return (!row.yhxxVO) ? "暂无" : row.yhxxVO.qyqc;
}

function sbjdFormatter(val, row) {
	return (!val) ? "暂无" : row.sbnf.toString()+"年第"+val + "季度";
}

function ztFormatter(val, row) {
	if(row.hzzt==0){
		return "待发起";
	}else if(row.hzzt==1){
		return "待比对";
	}
	if(row.hzzt==2){
		return "待审核";
	}else if(row.hzzt==3){
		return "通过";
	}else{
		return "未通过";
	}
}

function optFormatter(val, row) {
	var oprate = "";
	if(row.hzzt==2){
		oprate += "<div><a class='btn_blue06'  id='editbtn' onclick='javascript:details(\""+ row.hzdh + "\")' href='javascript:void(0)'>查看详情</a></div>";
		oprate += '<div><a class="btn_blue06"  id="editbtn" onclick="javascript:pass('
				+ row.id + ')" href="javascript:void(0)">通过</a>';
		oprate += '<a class="btn_blue06"  id="editbtn" onclick="javascript:reject('
				+ row.id + ')" href="javascript:void(0)">不通过</a></div>';
		return oprate;
	}else if(row.hzzt==3){
		oprate += "<div><a class='btn_blue06'  id='editbtn' onclick='javascript:details(\""+ row.hzdh + "\")' href='javascript:void(0)'>查看详情</a></div>";
	}else if(row.hzzt==4){
		oprate += "<div><a class='btn_blue06'  id='editbtn' onclick='javascript:details(\""+ row.hzdh + "\")' href='javascript:void(0)'>查看详情</a></div>";
	}
	return oprate;
}

function rqFormatter(val, row) {
	return (!val) ? "暂无" : backendCommon.dateTimeToLocalStringDays(val);
}

/**
 * 通过
 * 
 * @param id
 */
function pass(id) {
	$.messager.confirm('确认', '确认审核通过？', function(confirm) {
		if (confirm) {
			$.ajax({
				type : "POST",
				url : mainDomain + '/btsb/audit.action',
				data : {
					hzzt : 3,
					id : id
				},
				dataType : "JSON",
				success : function(result) {
					if (result.success) {
						if(result.data.status==-1){
							$.messager.alert('提示', '明细中存在不通过,请选择不通过!', 'info');
							return;
						}
						$('#yhxxDatagrid').datagrid('reload');
					} else {
						$.messager.alert('提示', result.message, 'info');
					}
				}
			});
		}
	});
}

/**
 * 拒绝
 * 
 * @param id
 */
function reject(id) {
	$.messager.confirm('确认', '确认审核拒绝？', function(confirm) {
		if (confirm) {
			$.ajax({
				type : "POST",
				url : mainDomain + '/btsb/audit.action',
				data : {
					hzzt : 4,
					id : id
				},
				dataType : "JSON",
				success : function(result) {
					if (result.success) {
						$('#yhxxDatagrid').datagrid('reload');
					} else {
						$.messager.alert('提示', result.message, 'info');
					}
				}
			});
		}
	});
}

/**
* 明细通过
* 
* @param id
*/
function setcross(hzdh,tlhph,hwydh) {
	$.messager.confirm('确认', '确认审核通过？', function(confirm) {
		if (confirm) {
			$.ajax({
				type : "POST",
				url : mainDomain + '/btsb/mxaudit.action',
				data : {
					hzdh : hzdh,
					tlhph : tlhph,
					hwydh : hwydh
				},
				dataType : "JSON",
				success : function(result) {
					if (result.success) {
						$('#ydDetails').dialog('close');
						$('#dgDetails').datagrid('reload');
					} else {
						$.messager.alert('提示', result.message, 'info');
					}
				}
			});
		}
	});
}

/**
* 质疑
* 
* @param id
*/
function setzy() {
	var hzdh = $('#hzdh').val();
	var tlhph = $('#tlhph').val();
	var hwydh = $('#hwydh').val();
	var data = $('input:radio:checked').val();
	if(data=="其他"){
		data = $('#othercontent').val();
	}
	if (data==null||data=="") {
		$.messager.alert('提示', "必须输入质疑内容!", 'info');
		return;
	}
	$.ajax({
		type : "POST",
		url : mainDomain + '/btsb/mxzy.action',
		data : {
			hzdh : hzdh,
			tlhph : tlhph,
			hwydh : hwydh,
			jjyy : data
		},
		dataType : "JSON",
		success : function(result) {
			if (result.success) {
				$.messager.alert('提示', '质疑成功', 'info');
				$('#ydDetails').dialog('close');
				$('#settjDlg').dialog('close');
				$('#dgDetails').datagrid('reload');
				$('#yhxxDatagrid').datagrid('reload');
				$('#hzdh').val('');
				$('#tlhph').val('');
				$('#hwydh').val('');
				$('#othercontent').val('');
				$('#oneradio').attr('selected','selected');
			} else {
				$.messager.alert('提示', result.message, 'info');
			}
		}
	});
}

function yddetails(hzdh,tlhph,hwydh) {
	var $win;
	$win = $('#ydDetails').window({
	    title: '运单明细',
	    width: 1020,
	    height: 450,
//	    top: ($(window).height() - 820) * 0.5,
//	    left: ($(window).width() - 450) * 0.5,
	    shadow: true,
	    modal: true,
//	    iconCls: 'icon-add',
	    closed: true,
	    minimizable: false,
	    maximizable: true,
	    maximized: true,  
	    collapsible: true
	});

	$win.window('open');
	//$('#ydDetails').window('open');
	$.ajax({
		type : "POST",
		url : mainDomain + '/btsb/getOneMxVO.action',
		data : {
			hzdh : hzdh,
			tlhph : tlhph,
			hwydh : hwydh
		},
		dataType : "JSON",
		success : function(result) {
			if (result.success) {
				var infotr = "<tr><td>运输类型</td><td>箱号</td><td>货物类型</td><td>海关申报单号</td><td>箱型</td>";
					infotr += "<td>出/入铁路站场时间</td><td>铁路货票号</td><td>发站</td><td>到站</td>";
					infotr += "<td>出/入港口时间</td><td>货物运单号</td><td>船舶到港</td><td>船舶离港</td><td>船名航次</td><td>MMSI号</td></tr>";
					if(result.data.sbmxinfoVO.length>0){
						var tlfj = result.data.sbmxinfoVO[0].tlzpmc;
//						if(result.data.sbmxinfoVO[0].jzxtlinfoVO!=null){
//							tlfj = result.data.sbmxinfoVO[0].jzxtlinfoVO.fjdz;
//						}
						var slfj = result.data.sbmxinfoVO[0].slzpmc;
//						if(result.data.sbmxinfoVO[0].jzxslinfoVO!=null){
//							slfj = result.data.sbmxinfoVO[0].jzxslinfoVO.fjdz;
//						}
						var hgfj = result.data.sbmxinfoVO[0].hgzpmc;
//						if(result.data.sbmxinfoVO[0].jzxhginfoVO!=null){
//							hgfj = result.data.sbmxinfoVO[0].jzxhginfoVO.fjdz;
//						}
						var hzdh = result.data.sbmxinfoVO[0].hzdh;
						var tlhph = result.data.sbmxinfoVO[0].tlhph;
						var hwydh = result.data.sbmxinfoVO[0].hwydh;
						var hgsbdh = result.data.sbmxinfoVO[0].hgsbdh;
						var ishc = result.data.sbmxinfoVO[0].ishc;
						$.each(result.data.sbmxinfoVO,function(i,v){
							if(v.yslx=="1"){
								infotr += "<tr><td>铁水</td>";
							}else if(v.yslx=="2"){
								infotr += "<tr><td>水铁</td>";
							}
							infotr += "<td>"+ v.xh +"</td>";
							infotr += "<td>"+ v.hwlxinfoVO.hwlxmc +"</td>";
							infotr += "<td>"+ v.hgsbdh +"</td>";
							infotr += "<td>"+ v.xxinfoVO.xxmc +"</td>";
							infotr += "<td>"+ backendCommon.dateTimeToLocalStringDays(v.crtlsj) +"</td>";
							infotr += "<td>"+ v.tlhph +"</td>";
							infotr += "<td>"+ v.fzinfoVO.zdmc +"</td>";
							infotr += "<td>"+ v.dzinfoVO.zdmc +"</td>";
							infotr += "<td>"+ backendCommon.dateTimeToLocalStringDays(v.crgksj) +"</td>";
							infotr += "<td>"+ v.hwydh +"</td>";
							infotr += "<td>"+ v.cbdginfoVO.gkmc +"</td>";
							infotr += "<td>"+ v.cblginfoVO.gkmc +"</td>";
							infotr += "<td>"+ v.cm + " " + v.hc +"</td>";
							infotr += "<td>"+ v.mmsi +"</td>";
							infotr += "</tr>";
						})
					}
					
					$('#ydtable').html(infotr);
					
					var oprate = '';
					if(ishc==0){
						oprate += '<a class="btn_blue06"  id="editbtn" onclick="javascript:setcross(\''
							+ hzdh + '\',\'' + tlhph + '\',\''+ hwydh + '\',\''+ hgsbdh + '\')" href="javascript:void(0)">通过</a>';
						oprate += '<a class="btn_blue06"  id="editbtn" onclick="javascript:showzy(\''
							+ hzdh + '\',\'' + tlhph + '\',\''+ hwydh + '\',\''+ hgsbdh + '\')" href="javascript:void(0)">质疑</a>';
					}
					$('#zydiv').html(oprate);
					
					if(tlfj!=null){
						$('#imageView_container1').html('<img id="Imgbox1" src=' + tlfj + ' rel=' + tlfj + ' />');
						$('#imageView_container1').imageView({width: '32%', height:500});
					}
					if(slfj!=null){
						$('#imageView_container2').html('<img id="Imgbox2" src=' + slfj + ' rel=' + slfj + ' />');
						$('#imageView_container2').imageView({width: '32%', height:500});
					}
					if(hgfj!=null){
						$('#imageView_container3').html('<img id="Imgbox3" src=' + hgfj + ' rel=' + hgfj + ' />');
						$('#imageView_container3').imageView({width: '32%', height:500});
					}else{
						$('#imageView_container3').html('');
						$('#imageView_container3').imageView({width: '32%', height:500});
					}
			}
		}
	});
}

function doSearchDetail(){
	$('#dgDetails').datagrid('load',{
		xh: $('#xh').val()
	});
}

/**
* 质疑选择
* 
* @param id
*/
function showzy(hzdh,tlhph,hwydh) {
	$('#settjDlg').dialog('open').dialog('center');
	$('#hzdh').val(hzdh);
	$('#tlhph').val(tlhph);
	$('#hwydh').val(hwydh);
}

